
package com.safebox.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import android.text.TextUtils;

import com.safebox.database.model.Account;
import com.safebox.database.model.Message;
import com.safebox.security.DataSecurity;
import com.safebox.utils.DateTimeUtils;

public class DatabaseHandler {
    public static final String TAG = "DatabaseHandler";
    private static DatabaseHandler sSingleton;

    private static DatabaseHelper mDatabaseHelper;

    private DatabaseHandler(Context context) {
        mDatabaseHelper = DatabaseHelper.getInstance(context);
    }

    public static synchronized DatabaseHandler getInstance(Context context) {
        if (sSingleton == null) {
            sSingleton = new DatabaseHandler(context);
        }
        return sSingleton;
    }

    public long insert(Message message) {
       
        SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues(4);
        contentValues.put(DatabaseHelper.MessagesColumns.ACCOUT_ID, message.getAccount_id());
        contentValues.put(DatabaseHelper.MessagesColumns.TITLE, message.getTitle()); 
        //encrypt content
        contentValues.put(DatabaseHelper.MessagesColumns.CONTENT, DataSecurity.desEncrypt(message.getContent()));
        contentValues.put(DatabaseHelper.MessagesColumns.DATE_CREATED, DateTimeUtils.getDateTime());
        contentValues.put(DatabaseHelper.MessagesColumns.DATE_MODIFIED, DateTimeUtils.getDateTime());

        return db.insert(DatabaseHelper.Tables.MESSAGES, null, contentValues);
    }
    
    public long insert(Account account)
    {
        SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues(4);
        contentValues.put(DatabaseHelper.AccountsColumns.USERNAME, account.getUsername());      
       
        contentValues.put(DatabaseHelper.AccountsColumns.PASSWORD, DataSecurity.md5(account.getPassword()));
             
        contentValues.put(DatabaseHelper.AccountsColumns.DATE_CREATED, DateTimeUtils.getDateTime());
        contentValues.put(DatabaseHelper.AccountsColumns.DATE_MODIFIED, DateTimeUtils.getDateTime());

        return db.insert(DatabaseHelper.Tables.ACCOUNTS, null, contentValues);
    }

    /**
     * Query all the data from data table specified, all the cursor should be
     * closed once out of use.
     * 
     * @param tableName The table name
     * @return cursor The query results
     */
    public Cursor query(String tableName) {
        SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
        String sql = "SELECT * FROM " + tableName + " ORDER BY _id DESC ";
        return db.rawQuery(sql, null);
    }

    /**
     * Query all the data from data table specified, all the cursor should be
     * closed once out of use.
     * 
     * @param tableName The table name
     * @return cursor The query results
     */
    public Cursor query(String tableName, long id) {
        SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
        String sql = "SELECT * FROM " + tableName + " WHERE _id=" + id + " ORDER BY _id DESC ";
        return db.rawQuery(sql, null);
    }
    
  
    
    //general method
    public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
    {
        SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
        if (TextUtils.isEmpty(orderBy))
            orderBy = " _id DESC ";
        return db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
    }

    /**
     * @param tableName DatabaseHelper.Tables.MESSAGES
     *            DatabaseHelper.Tables.MAXIMS DatabaseHelper.Tables.CONFERENCE
     * @param id The row to delete
     * @return he number of rows affected if a whereClause is passed in, 0
     *         otherwise. To remove all rows and get a count pass "1" as the
     *         whereClause.
     */
    public int delete(String tableName, long id) {
        SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
        return db.delete(tableName, BaseColumns._ID + "=" + id, null);
    }

    /**
     * @param tableName DatabaseHelper.Tables.MESSAGES
     *            DatabaseHelper.Tables.MAXIMS DatabaseHelper.Tables.CONFERENCE
     * @param whereClause the optional WHERE clause to apply when deleting.
     *            Passing null will delete all rows.
     * @param whereArgs
     * @return the number of rows affected if a whereClause is passed in, 0
     *         otherwise. To remove all rows and get a count pass "1" as the
     *         whereClause.
     */
    public int delete(String tableName, String whereClause, String[] whereArgs) {
        SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
        return db.delete(tableName, whereClause, whereArgs);
    }

    /** for update */
    public int update(String tableName, Message message, long id) {
        SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues(3);

        contentValues.put(DatabaseHelper.MessagesColumns.TITLE, message.getTitle());
        contentValues.put(DatabaseHelper.MessagesColumns.CONTENT, DataSecurity.desEncrypt(message.getContent()));      
        contentValues.put(DatabaseHelper.MessagesColumns.DATE_MODIFIED, DateTimeUtils.getDateTime());
        return db.update(tableName, contentValues, BaseColumns._ID + "=" + id, null);
    }
    
    public int update(String tableName, Account account, long id) {
        SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
        ContentValues contentValues = new ContentValues(3);

        contentValues.put(DatabaseHelper.AccountsColumns.USERNAME, account.getUsername());
        contentValues.put(DatabaseHelper.AccountsColumns.PASSWORD, account.getPassword());      
        contentValues.put(DatabaseHelper.AccountsColumns.DATE_MODIFIED, DateTimeUtils.getDateTime());
        return db.update(tableName, contentValues, BaseColumns._ID + "=" + id, null);
    }

    /**
     * Raw query.
     * 
     * @param sql
     * @param selectionArgs
     * @return
     */
    public Cursor rawQuery(String sql, String[] selectionArgs) {
        SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
        return db.rawQuery(sql, selectionArgs);
    }
  
    

}
